package sad.visao;

import java.awt.Color;
import javax.swing.JOptionPane;
import sad.util.DAOEvent;


public class CadProfessorDialog extends DialogAbstrato {
/*Classe responsavel pelo cadastro ou edição do professor
  ela é instanciada apartir da ação do obj btProfessor ou do novoProfessor
  na classe SadGui*/

    public CadProfessorDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(parent);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        saveProfessor = new javax.swing.JPanel();
        txtNome = new javax.swing.JTextField();
        txtLogin = new javax.swing.JTextField();
        txtSenha = new javax.swing.JTextField();
        lblNome = new javax.swing.JLabel();
        lblLogin = new javax.swing.JLabel();
        lblSenha = new javax.swing.JLabel();
        lblMessage = new javax.swing.JLabel();
        btnCadastrar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SAD - Cadastro");

        saveProfessor.setBorder(javax.swing.BorderFactory.createTitledBorder("Salvar Professor"));

        txtNome.setFocusCycleRoot(true);
        txtNome.setFocusTraversalPolicyProvider(true);

        txtLogin.setFocusTraversalPolicyProvider(true);

        lblNome.setText("Nome:");

        lblLogin.setText("Login:");

        lblSenha.setText("Senha:");

        javax.swing.GroupLayout saveProfessorLayout = new javax.swing.GroupLayout(saveProfessor);
        saveProfessor.setLayout(saveProfessorLayout);
        saveProfessorLayout.setHorizontalGroup(
            saveProfessorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(saveProfessorLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(saveProfessorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblMessage, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, saveProfessorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(lblLogin)
                        .addComponent(lblSenha)
                        .addComponent(txtSenha)
                        .addComponent(txtLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                        .addComponent(lblNome)
                        .addComponent(txtNome)))
                .addContainerGap())
        );
        saveProfessorLayout.setVerticalGroup(
            saveProfessorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, saveProfessorLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblNome)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblLogin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblSenha)
                .addGap(5, 5, 5)
                .addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38))
        );

        btnCadastrar.setText("Cadastrar");
        btnCadastrar.setActionCommand("Salvar Professor");

        btnCancelar.setText("Cancelar");
        btnCancelar.setActionCommand("Cancelar Professor");

        btnExcluir.setText("Excluir");
        btnExcluir.setVisible(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(saveProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCadastrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnExcluir, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(saveProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(btnCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        btnExcluir.getAccessibleContext().setAccessibleName("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCadastrar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JLabel lblLogin;
    private javax.swing.JLabel lblMessage;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblSenha;
    private javax.swing.JPanel saveProfessor;
    private javax.swing.JTextField txtLogin;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtSenha;
    // End of variables declaration//GEN-END:variables

   // <editor-fold defaultstate="collapsed" desc="Responde a eventos DAO">

    @Override
    public void modelDAOChange(DAOEvent evt) {
        switch(evt.getID()){
           case DAOEvent.DAO_CREATE:
                resetDialog();
                lblMessage.setForeground(Color.GREEN);
                lblMessage.setText("Professor"
                    + " cadastrado com sucesso!!!");
                break;
               
           case DAOEvent.DAO_EXISTE:
               JOptionPane.showMessageDialog(this, "O usuário de login: "
                    + evt.getField() + " já existe!!!", null, 
                                            JOptionPane.WARNING_MESSAGE);
                resetDialog();
                break;

           case DAOEvent.DAO_DELETE:
               lblMessage.setForeground(Color.GREEN);
               lblMessage.setText("Professor"
                    + " deletado com sucesso!!!");
               break;
               
           case DAOEvent.DAO_UPDATE:
               lblMessage.setForeground(Color.GREEN);
               lblMessage.setText("Professor"
                    + " atualizado com sucesso!!!");
               break;
               
           case DAOEvent.DAO_ERROFIELD:
               JOptionPane.showMessageDialog(this, "O campo " + evt.getField()
                       + " não pode ficar em branco!!!");
               focusField(evt.getField());
               break;
        }
    }// </editor-fold>

   // <editor-fold defaultstate="collapsed" desc="Retorna dados do objeto">
    public String getNome() {
        return this.txtNome.getText();
    }

    public String getLogin() {
        return this.txtLogin.getText();
    }

    public String getSenha() {
        return this.txtSenha.getText();
    }// </editor-fold>

   //<editor-fold defaultstate="collapsed" desc="Métodos públicos comuns a todos os Dialogs">
    @Override
    public void addOuvintes() {
        btnCadastrar.addActionListener(getSuporteEventos());
        btnExcluir.addActionListener(getSuporteEventos());
        btnCancelar.addActionListener(getSuporteEventos());
        this.addWindowOuvinte(getSuporteEventos());
    }
    
    @Override
    public void close() {
        this.resetDialog();
        this.dispose();
    }
    
    @Override
    public void setEditableDados(Object[] o){
        if(o!=null){
            txtNome.setText(o[0].toString());
            txtLogin.setText(o[1].toString());
            txtLogin.setEnabled(false);
            txtSenha.setText(o[2].toString());
            btnExcluir.setVisible(true);
            btnCadastrar.setText("Alterar");
            btnCadastrar.setActionCommand("Alterar");
            saveProfessor.setBorder(
                    javax.swing.BorderFactory.createTitledBorder("Alterar Professor"));
            btnCancelar.setText("Voltar");
        }
} //</editor-fold>
 
   //<editor-fold defaultstate="collapsed" desc="Métodos privados, mas comuns a todos os Dialogs">
    @Override
 protected void resetDialog() {
     txtNome.setText(null);
     txtLogin.setText(null);
     txtSenha.setText(null);
     lblMessage.setText(null);
     txtNome.requestFocusInWindow();
 }
 
    @Override
 protected void focusField(String field) {
     if(field.equals("Nome")){
         txtNome.requestFocusInWindow();
     }
     if(field.equals("Login")){
         txtLogin.requestFocusInWindow();
     }
     if(field.equals("Senha")){
         txtSenha.requestFocusInWindow();
     }
 }
 //</editor-fold>
}
/*Pelo que eu to entendendo está sendo instanciado mais de um suporteEvento*/